package cn.minimelon.cushaw.view.datadef;

import cn.minimelon.bittermelon.bean.AjaxResult;
import cn.minimelon.bittermelon.bean.LoginUser;
import cn.minimelon.bittermelon.solon.BaseController;
import cn.minimelon.bittermelon.utils.Melon;
import cn.minimelon.cushaw.entity.SysDatadef;
import cn.minimelon.cushaw.model.DatadefReq;
import cn.minimelon.cushaw.service.SysDatadefService;
import org.beetl.sql.core.page.DefaultPageRequest;
import org.beetl.sql.core.page.PageRequest;
import org.beetl.sql.core.page.PageResult;
import org.beetl.sql.mapper.annotation.Update;
import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.Context;

@Controller
@Mapping("/a/dataDef/structure")
public class DefController extends BaseController {
    @Inject
    SysDatadefService datadefService;

    @Get
    @Mapping("/{uid}")
    public AjaxResult<SysDatadef> select(String uid) {
        SysDatadef datadef = datadefService.selectById(uid);
        return Melon.success(datadef);
    }

    @Get
    @Mapping("/one/{code}")
    public AjaxResult<SysDatadef> selectByCode(String code) {
        SysDatadef datadef = datadefService.selectByCode(code);
        return Melon.success(datadef);
    }

    @Get
    @Mapping("/list/{areaCode}")
    public AjaxResult<Object> list(String areaCode) {
        return null;
    }

    @Get
    @Mapping("/children/{parentCode}")
    public AjaxResult<Object> children(String parentCode) {
        return null;
    }

    @Get
    @Mapping("/search")
    public AjaxResult<Object> search(DatadefReq req) {
        PageResult<SysDatadef> pr = datadefService.selectPage(req);
        return Melon.success(pr);
    }

    @Post
    @Mapping("/save")
    public AjaxResult<Object> save(SysDatadef datadef) {
        LoginUser user = this.getLoginUser();
        datadefService.insert(datadef, user);
        return Melon.success();
    }

    @Post
    @Mapping("/update")
    public AjaxResult<Object> update(SysDatadef datadef) {
        LoginUser user = this.getLoginUser();
        int count = datadefService.update(datadef, user);
        if (count > 0) {
            return Melon.success();
        }
        return Melon.error("UPDATE_FAILURE");
    }

    @Delete
    @Mapping("/{uid}")
    public AjaxResult<Object> delete(String uid) {
        return null;
    }
}
